//
// Created by mmuee on 2024/4/23.
//

#ifndef YWZM_BYTEBUFFER_HPP
#define YWZM_BYTEBUFFER_HPP

#include "ConstDefine.hpp"

class ByteBuffer
{
    // false:内存内部管理, true:内存外部管理,只做访问操作
    bool _unmanaged{false};
    uint8_t* _data{};
    int64_t _size{};
    int64_t _capacity{}; //< 申请的内存容量

public:
    ByteBuffer();
    ByteBuffer(ByteBuffer const &buffer);
    explicit ByteBuffer(uint64_t size);
    ByteBuffer(uint64_t size, void* data, bool unmanaged);
    ByteBuffer& operator=(ByteBuffer&& other) noexcept; //< 移动构造
    ByteBuffer& operator=(const ByteBuffer& other);     //< 拷贝构造

    [[nodiscard]] inline const int64_t& size() const { return this->_size; }
    [[nodiscard]] inline bool isValid() const { return _data != nullptr && _size > 0; }
    inline uint8_t* data() const { return this->_data; }
    inline void obtain(void** data) const { *data = this->_data; }
    void resize(uint64_t newSize);
    void clean();
};


#endif //YWZM_BYTEBUFFER_HPP
